package cn.com.oceansoft.business.base.controller;

import cn.com.oceansoft.nos.common.constant.ConfigInfo;
import cn.com.oceansoft.nos.common.constant.Constant;
import cn.com.oceansoft.nos.common.editor.DateEditor;
import cn.com.oceansoft.nos.common.editor.DoubleEditor;
import cn.com.oceansoft.nos.common.editor.IntegerEditor;
import cn.com.oceansoft.nos.common.editor.LongEditor;
import cn.com.oceansoft.nos.common.util.ServerInfo;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.DictItem;
import cn.com.oceansoft.business.sys.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * 控制器基类<br/>提供基本类型数据转换、异常信息捕获、属性文件读取功能
 */
public abstract class BaseController {

    private static final Logger log = LoggerFactory.getLogger(BaseController.class);

    public static final String SIMPLE_DATE = new SimpleDateFormat("yyyyMMdd").format(new Date());
    @Resource
    protected ConfigInfo configInfo;

    @Resource
    private HttpServletRequest request;


    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(int.class, new IntegerEditor());
        binder.registerCustomEditor(long.class, new LongEditor());
        binder.registerCustomEditor(double.class, new DoubleEditor());
        binder.registerCustomEditor(Date.class, new DateEditor());
    }

    /**
     * 记录操作日志
     *
     * @param message 日志内容
     */
    protected void log(String message) {
        request.setAttribute(Constant.OPERATION_LOG_CONTENT, message);
    }

    /**
     * 保存登录用户会话信息
     *
     * @param sysUser 待存储于session数据
     */
    public void saveSession(SysUser sysUser) {
        request.getSession().setAttribute(Constant.SESSION_USER, sysUser);
    }


    /**
     * 获取当前登录用户Session信息
     *
     * @return SysUser
     */
    protected SysUser getSession() {
        Object sessionObj = request.getSession().getAttribute(Constant.SESSION_USER);
        if (null != sessionObj && sessionObj instanceof SysUser)
            return (SysUser) sessionObj;
        return null;
    }



    /**
     * 保存用户权限信息
     *
     * @param resources 权限对应的资源集合
     */
    public void savePrivilege(Set<String> resources) {
        if (null != resources)
            request.getSession().setAttribute(Constant.SESSION_USER_PRIVILEGE, resources);
    }

    /**
     * 获取字典项
     *
     * @param dictKey 字典项KEY
     * @return 字典Value
     */
    protected DictItem getDictItem(String dictKey) {
        return (DictItem) request.getSession().getServletContext().getAttribute(dictKey);
    }

    /**
     * 更新上下文里字典数据
     *
     * @param dictItem 字典对象
     */
    protected void updateDictItem(DictItem dictItem) {
        request.getSession().getServletContext().setAttribute(dictItem.getKey(), dictItem);
    }

    /**
     * 获取同类型字典项集合
     *
     * @param dictGroup 字典类型ID
     * @return List<DictItem>
     */
    protected List<DictItem> getDictItems(String dictGroup) {
        return new ArrayList<DictItem>();
    }



    @ExceptionHandler(Exception.class)
    public ModelAndView exception(Exception e, HttpServletRequest request) {
        request.setAttribute("exception", e);
        if (ServerInfo.isAjax(request) || request.getParameter("ajax") != null) {
            return ajaxDoneError(e.getMessage());
        }
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("statusCode", 300);
        mav.addObject("message", e.getMessage());
        e.printStackTrace();
        return mav;
    }

    protected ModelAndView ajaxDone(int statusCode, String message, String forwardUrl) {
        ModelAndView mav = new ModelAndView("ajaxDone");
        mav.addObject("statusCode", statusCode);
        mav.addObject("message", message);
        mav.addObject("forwardUrl", forwardUrl);
        return mav;
    }

    protected String getRequestUrl() {
        return String.format("http://%s:%d%s", request.getServerName(), request.getServerPort(), request.getRequestURI());
    }

    protected String getRequestHost() {
        return request.getRemoteHost();
    }

    /**
     * 获取本机IP地址
     *
     * @return 本地IP地址
     */
    protected String getLocalHostIP() {
        return request.getLocalAddr();
    }

    protected ModelAndView ajaxDoneError(String message) {
        return ajaxDone(300, message, "");
    }


    /**
     * 返回JSONP结果
     *
     * @param result 数据对象
     * @return ModelAndView
     */
    protected ModelAndView JSONP(final Result result) {
        return new ModelAndView("jsonPView", "result", result);
    }



}
